﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.Vector2ErrorCorrector
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// Handles prediction error correction smoothing for a Vector2.
/// </summary>
[Serializable]
public struct Vector2ErrorCorrector
{
  public Vector2 Vector2PreRollback;
  public Vector2 Vector2Error;

  public void Init(Vector2 value)
  {
    this.Vector2PreRollback = value;
    this.Vector2Error = new Vector2();
  }

  public void OnPreRollback(Vector2 value) => this.Vector2PreRollback = value;

  public void OnPostResimulation(Vector2 currentValue)
  {
    this.Vector2Error += this.Vector2PreRollback - currentValue;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public void Render(
    ref Vector2 interpolatedValue,
    float correctionMultiplier,
    float teleportThreshold,
    float deltaTime)
  {
    this.Vector2Error *= Mathf.Exp(-correctionMultiplier * deltaTime);
    if ((double) this.Vector2Error.magnitude > (double) teleportThreshold)
      this.Vector2Error = new Vector2();
    interpolatedValue += this.Vector2Error;
  }
}
